package vn.ebuzz.sms.core;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import vn.ebuzz.sms.worker.IncomingQueue;
import vn.ebuzz.sms.worker.OutgoingQueue;
import vn.ebuzz.sms.worker.ProcessQueue;
import vn.ebuzz.sms.worker.PushQueue;


public class AppListener implements ServletContextListener{		
	public void contextDestroyed(ServletContextEvent arg0) {
		IncomingQueue.getInstance().stop();
		OutgoingQueue.getInstance().stop();
		ProcessQueue.getInstance().stop();
		Database.releasePool();
	}

	public void contextInitialized(ServletContextEvent arg0) {
		String rootPath = arg0.getServletContext().getRealPath("/");
		Configuration config = Configuration.getInstance();
		config.setRootPath(rootPath);

		try {
			Properties prop = config.loadFile("config.properties");
			Configuration.CPID = prop.getProperty("CPID");
			Configuration.CPKEY = prop.getProperty("CPKEY");
			Configuration.MAX_THREAD = Integer.parseInt(prop.getProperty("MAX_THREAD"));
			Configuration.MAX_BATCH = Integer.parseInt(prop.getProperty("MAX_BATCH"));
			Configuration.SLEEP = Integer.parseInt(prop.getProperty("SLEEP"));
			Configuration.THRESHOLD = Integer.parseInt(prop.getProperty("THRESHOLD"));
			Configuration.SEPERATOR = Integer.parseInt(prop.getProperty("SEPERATOR"));
			Configuration.VIVASURL = prop.getProperty("VIVASURL");		
			
			Configuration.MDOORURL = prop.getProperty("MDOORURL");
			Configuration.MDOORPW = prop.getProperty("MDOORPW");
			String tmp = prop.getProperty("MDOORKEY");
			if(tmp.contains(","))
				Configuration.MDOORKEY = tmp.split(",");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		IncomingQueue.getInstance();
		OutgoingQueue.getInstance();
		ProcessQueue.getInstance();
		PushQueue.getInstance();
	}
}
